package com.pets.springboot_petshop.mapper;

import com.pets.springboot_petshop.entity.Pettype;
import com.pets.springboot_petshop.entity.Serviceappointment;
import com.pets.springboot_petshop.entity.Shoppingcart;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/15 上午10:55 周二
 * @Description: 数据访问层接口
 */
@Mapper
@Repository
public interface ShoppingcartMapper {
    //id查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.quantity, " +
            "a.product_id, " +
            "a.check," +
            "c.name, " +
            "c.original_price, " +
            "c.discount_price, " +
            "c.img, " +
            "a.create_time " +
            "FROM shoppingcart a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN product c ON a.product_id = c.id " +
            "WHERE a.id = #{id}")
    Shoppingcart select(int id);
    //user_id查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.quantity, " +
            "a.product_id, " +
            "c.name, " +
            "a.check," +
            "c.original_price, " +
            "c.discount_price, " +
            "c.img, " +
            "a.create_time " +
            "FROM shoppingcart a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN product c ON a.product_id = c.id " +
            "WHERE a.user_id = #{user_id}")
    List<Shoppingcart> selectuser_id(int user_id);
    //product_id查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.quantity, " +
            "a.product_id, " +
            "c.name, " +
            "a.check," +
            "c.original_price, " +
            "c.discount_price, " +
            "c.img, " +
            "a.create_time " +
            "FROM shoppingcart a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN product c ON a.product_id = c.id " +
            "WHERE a.product_id = #{product_id}")
    List<Shoppingcart> selectproduct_id(int product_id);
    //查询所有
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.quantity, " +
            "a.product_id, " +
            "c.name, " +
            "a.check," +
            "c.original_price, " +
            "c.discount_price, " +
            "c.img, " +
            "a.create_time " +
            "FROM shoppingcart a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN product c ON a.product_id = c.id ;")
    List<Shoppingcart> findAll();
    //用户购物车check查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.quantity, " +
            "a.product_id, " +
            "c.name, " +
            "a.check," +
            "c.original_price, " +
            "c.discount_price, " +
            "c.img, " +
            "a.create_time " +
            "FROM shoppingcart a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN product c ON a.product_id = c.id " +
            "WHERE a.user_id=#{user_id} AND a.`check`=#{check}")
    List<Shoppingcart> selectcheck(Shoppingcart shoppingcart);
    //添加
    @Insert("insert into shoppingcart(user_id,product_id,quantity,`check`,create_time,update_time)values(#{user_id},#{product_id},#{quantity},1,now(),now());")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Shoppingcart shoppingcart);
    //修改数据
    @Update("UPDATE shoppingcart SET user_id=#{user_id},product_id=#{product_id},quantity=#{quantity},update_time=now() where id= #{id};")
    int update(Shoppingcart shoppingcart);
    //修改数量
    @Update("UPDATE shoppingcart SET quantity=#{quantity},update_time=now() where user_id=#{user_id} AND id= #{id};")
    int updatequantity(Shoppingcart shoppingcart);
    //修改状态
    @Update("UPDATE shoppingcart SET `check`=#{check},update_time=now() where user_id=#{user_id} AND id= #{id};")
    int updatecheck(Shoppingcart shoppingcart);
    //全选
    @Update("UPDATE shoppingcart SET `check` = #{check}, update_time = NOW() WHERE user_id = #{user_id}")
    int updateCheckForAll(@Param("user_id") int user_id, @Param("check") String check);


    //搜索用户以及用户商品有没有同时相等的
    @Select("SELECT * FROM shoppingcart WHERE user_id=#{user_id} AND product_id=#{product_id}")
    Shoppingcart selectupdate (Shoppingcart shoppingcart);
    //根据user_id进行删除
    @Delete("DELETE FROM shoppingcart WHERE user_id = #{user_id} AND `check` = 1")
    int delete(int user_id);
}
